package com.sansui.blog.controller.admin.page;

import com.sansui.blog.pojo.User;
import com.sansui.blog.pojo.UserLoginLog;
import com.sansui.blog.service.BlogService;
import com.sansui.blog.service.CommentService;
import com.sansui.blog.service.SysInfoService;
import com.sansui.blog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @name: IndexController
 * @author: Sansui
 * @Software: IDEA
 * @Blog: https://www.cnblogs.com/sansui6/
 * @date: 2022-03-20 22:47
 **/
//后台首页
@Controller
@RequestMapping("/admin")
public class IndexController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private UserService userService;

    @Autowired
    private SysInfoService sysInfoService;

    @Autowired
    private CommentService commentService;

    // 后台index页面
    @GetMapping("page/index")
    public ModelAndView adminIndex(ModelAndView mv,HttpSession session){
//        User user = (User) session.getAttribute("user");
        Map<String,Object> map = (Map<String, Object>) session.getAttribute("user");
        Map<String,Object> data = blogService.countBlogInfo();
        Map<String,Object> sysinfo = sysInfoService.getSysInfo();
        
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        mv.setViewName("admin/page/index");
        mv.addObject("username",map.get("username"));
        mv.addObject("nowtime",df.format(new Date()));
        mv.addObject("countblog",data.get("countblog"));
        mv.addObject("countviews",data.get("countviews"));
        mv.addObject("countcomments",commentService.countcomments());
        mv.addObject("webname",sysinfo.get("webname"));
        mv.addObject("versionnum",sysinfo.get("versionnum"));
        mv.addObject("weburl",sysinfo.get("weburl"));
        return mv;
    }

    @PostMapping("page/login_info")
    @ResponseBody
    public Map<String, Object> loginInfo(){
        Map<String, Object> map = userService.getLoginLog();
        return map;
    }





}
